package com.lottery.ruok.lottrey.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.activity.BindBankActivity;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetBanksRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetBanksResponse;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import utils.Utils;

/**
 * Created by ASUS on 2017/8/2.
 */

public class ChooseBankDialog extends DialogFragment {
    @BindView(R.id.close)
    ImageView close;
    @BindView(R.id.listview)
    ListView listview;

    private BindBankActivity bindBankActivity;
    private ArrayList<GetBanksResponse> getBanksResponses;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //自定义对话框样式
        View v = inflater.inflate(R.layout.choose_bank_layout, null);

        ButterKnife.bind(this, v);

        Window window = getDialog().getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);

        getDialog().setCanceledOnTouchOutside(false);

        getList();

        return v;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if(activity instanceof BindBankActivity){
            bindBankActivity = (BindBankActivity)activity;
        }
    }

    private void getList() {
        RetrofitBase.getRetrofit().create(GetBanksRequest.class).getResult()
                .compose(RxHelper.<ArrayList<GetBanksResponse>>handleResult())
                .subscribe(new RxSubscribe<ArrayList<GetBanksResponse>>(getActivity()) {
            @Override
            protected void _onNext(ArrayList<GetBanksResponse> g) {
                getBanksResponses = g;

                MyAdapter myAdapter = new MyAdapter();
                listview.setAdapter(myAdapter);
                listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        bindBankActivity.setBank(getBanksResponses.get(i).getBankname());

                        dismissAllowingStateLoss();
                    }
                });
            }

            @Override
            protected boolean showDialog() {
                return true;
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();

        Window window = getDialog().getWindow();
        window.setLayout(Utils.getWindowDisplay(getActivity()).widthPixels, Utils.getWindowDisplay(getActivity()).heightPixels - Utils.dip2px(getActivity(), 80));
    }

    @OnClick(R.id.close)
    public void onViewClicked() {
        dismissAllowingStateLoss();
    }

    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return getBanksResponses.size();
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view != null) {
                holder = (ViewHolder) view.getTag();
            } else {
                view = LayoutInflater.from(getContext()).inflate(R.layout.bank_item, null);
                holder = new ViewHolder(view);

                view.setTag(holder);
            }

            holder.name.setText(getBanksResponses.get(i).getBankname());

            return view;
        }

        class ViewHolder {
            @BindView(R.id.name)
            TextView name;

            ViewHolder(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
}